/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IResolveResult;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 resolve' command.
  * 
  * @version $Revision: 169 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Resolve extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables

	/** Stores the operation's result. */	
	private List result = new ArrayList();

	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Resolve. 
	  *
	  * @return the new Reopen instance.
	  */
	public static final Resolve newInstance()
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
		return new Resolve(status);
	}

	// ------------------------------------------------------------------------ Private Constructors

	/** Creates a new instance. 
	  * 
	  * @param status the MultiStatus instance.
	  */
	private Resolve(MultiStatus status)
	{
		super(status);
	}	

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)

	/** @see IClientUser#outputInfo(char, String) */
	public void outputInfo(char level, String infoString)
	{
		if (level != '0')
		{
			return;
		}
			
		int pos = infoString.lastIndexOf(" - ");                                       //$NON-NLS-1$
		if (pos != -1)
		{
			Result resolveResult = new Result();
			resolveResult.setLocalFilename(infoString.substring(0, pos));
			
			int pos2 = infoString.indexOf("//", pos);			
			pos = infoString.indexOf('#', pos);
			if (pos != -1)
			{
				String depotFilename = infoString.substring(pos2, pos);
				resolveResult.setDepotFilename(depotFilename);
				
				pos++;
				pos2 = infoString.indexOf(',', pos);
				if (pos2 == -1)
				{
					pos2 = infoString.length();
				}
				
				Integer revision = new Integer(infoString.substring(pos, pos2));
				resolveResult.setRevision(revision); 
				
				result.add(resolveResult);
			}
		}
	}

	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "resolve";                                                              //$NON-NLS-1$
	}

	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}	
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}	
		
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Returns the result of the operation.
	  * 
	  * @return array of result objects.
	  */
	public IResolveResult[] getResult()
	{
		return (IResolveResult[]) result.toArray(new IResolveResult[result.size()]);
	}

	// ---------------------------------------------------------------------- Private Nested Classes

	/**
	  * Implementation of the IResolveResult interface. Stores information
	  * provided by the 'p4 resolve' command. 
	  * 
	  * @see IResolveResult
	  */
	public class Result implements IResolveResult
	{
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the depot filename. */
		private String depotFilename;
		/** Stores the filename. */
		private String filename;
		/** Stores the revision. */
		private Integer revision; 
		
		// -------------------------------------------------------------------------- Public Methods
		
		/** @see IResolveResult#getDepotFilename() */
		public String getDepotFilename() 
		{
			return depotFilename;
		}

		
		/** @see IResolveResult#getLocalFilename() */
		public String getLocalFilename()
		{
			return filename;
		}
		
		/** @see IResolveResult#getRevision() */
		public Integer getRevision() 
		{
			return revision;
		}
		
		// --------------------------------------------------------------------------------- Methods
		
		/** Sets the depot filename.
		  *
		  * @param newDepotFilename the new depot filename. 
		  */
		void setDepotFilename(String newDepotFilename)
		{
			depotFilename = newDepotFilename;
		}
		
		/** Sets the filename.
		  * 
		  * @param filename The filename to set
		  */
		void setLocalFilename(String filename) 
		{
			this.filename = filename;
		}
		
		/** Sets the revision to resolve.
		  *
		  * @param theRevision the revision value. 
		  */		
		void setRevision(Integer theRevision)
		{
			revision = theRevision;
		}
	}	
}
